<template>
	<view class="body-view">
		<!--导航和状态栏-->
		<view class="state-navigation">
			<view class="slim_header_navigation d-flex a-center j-center" >
				<view class="left_box" @tap="goBack">
					<image src="/static/center/back.png" class="back"></image>
				</view>
				<view class="title">创建方案</view>
				<view class="right_box">
					<!-- <image src="/static/center/dian.png" class="dian"></image> -->
				</view>
			</view>
		</view>
		<!-- 顶部位置占位 -->
		<view style="height:calc(120rpx + var(--status-bar-height));"></view>
		<scroll-view scroll-y="true" style="height: calc(100vh - 120rpx - var(--status-bar-height));">
			<view class="content">
				<uni-forms ref="form" err-show-type="toast" :modelValue="formData" :rules="rules">
					<view class="content-form">
						<uni-forms-item class="form-item" name="name">
							<view class="form-item-title d-flex a-center">
								<view class="item-title-left"></view>
								<view class="item-title-text">方案名称</view>
							</view>
							<input :adjust-position="false" class="content-input" placeholder-class="placeholder-class" v-model="formData.name" placeholder="最多20个字"/>
						</uni-forms-item>
						<uni-forms-item class="form-item" name="introduction">
							<view class="form-item-title d-flex a-center">
								<view class="item-title-left"></view>
								<view class="item-title-text">方案介绍</view>
							</view>
							<textarea :maxlength="200" :adjust-position="false" class="content-input" style="width: 100%;height: 300rpx;padding-top: 20rpx;padding-bottom: 20rpx;" placeholder-class="placeholder-class" v-model="formData.introduction" :placeholder="`1、请简单介绍方案的特点。\n2、请输入食物的食用量，运动量等，30-200字`"/>
						</uni-forms-item>
					</view>
					<view style="height: 20rpx;"></view>
					<view class="content-form">
						<uni-forms-item class="form-item" name="bmi">
							<view class="form-item-title d-flex a-center j-between">
								<view class="d-flex a-center">
									<view class="item-title-left"></view>
									<view class="item-title-text">适用范围</view>
								</view>
								<view @tap="showBmi" class="d-flex a-center">
									<i class="icon icon-jisuanqi" style="color: #1DC691;font-size: 30rpx;"></i>
									<view class="item-title-describe">BMI计算对照</view>
								</view>
							</view>
							<view class="form-item-value">{{formData.bmi}}</view>
							<view class="form-item-tip">请点击选择BMI段</view>
							<view class="form-item-BMI d-flex j-between">
								<view class="info-chart-1" @tap="onBMI(0)">
									<view class="chart-v" :style="{background:isBMI(0)?'#65E7E8':'#F5F5F5'}"></view>
									<view class="info-chart-describe">{{bmiList[0]}}偏瘦</view>
								</view>
								<view class="info-chart-2" @tap="onBMI(1)">
									<view class="chart-v" :style="{background:isBMI(1)?'#7FF0AC':'#F5F5F5'}"></view>
									<view class="info-chart-describe">{{bmiList[1]}}正常</view>
								</view>
								<view class="info-chart-3" @tap="onBMI(2)">
									<view class="chart-v" :style="{background:isBMI(2)?'#FFD401':'#F5F5F5'}"></view>
									<view class="info-chart-describe">{{bmiList[2]}}偏重</view>
								</view>
								<view class="info-chart-4" @tap="onBMI(3)">
									<view class="chart-v" :style="{background:isBMI(3)?'#FF9858':'#F5F5F5'}"></view>
									<view class="info-chart-describe">{{bmiList[3]}}超重</view>
								</view>
								<view class="info-chart-5" @tap="onBMI(4)">
									<view class="chart-v" :style="{background:isBMI(4)?'#FF6B4F':'#F5F5F5'}"></view>
									<view class="info-chart-describe">{{bmiList[4]}}严重超重</view>
								</view>
							</view>
							<view class="item-BMI-algorithm">BMI体质指数=体重(千克)/身高(米)的平方</view>
						</uni-forms-item>
						<uni-forms-item class="form-item" name="heatIsPoor">
							<view class="form-item-title d-flex a-center">
								<view class="item-title-left"></view>
								<view class="item-title-text">推荐热量差</view>
							</view>
							<view class="item-list d-flex j-between">
								<view class="item-list-left d-flex d-c a-center j-center">
									<i class="icon icon-reliangcha" style="color: #333333;font-size: 40rpx;"></i>
									<view class="list-left-title">热量差</view>
								</view>
								<view  @tap="showCaloriePopup" v-if="formData.heatIsPoor==''" class="item-list-right d-flex j-end">
									<view class="list-right-add d-flex a-center">
										<i class="icon icon-tianjia" style="color: #1DC691;font-size: 50rpx;"></i>
									</view>
								</view>
								<view @tap="showCaloriePopup" v-else class="item-list-right d-flex">
									<view class="d-flex d-c a-center j-center">
										<view class="d-flex">
											<view class="list-right-number">{{formData.heatIsPoor}}</view>
											<view class="list-right-unit">千卡</view>
										</view>
										<view class="list-right-describe">{{heatIsPoorDescribe}}</view>
									</view>
								</view>
							</view>
							<view style="height: 58rpx;"></view>
						</uni-forms-item>
					</view>
					<view style="height: 20rpx;"></view>
					<view class="content-form">
						<uni-forms-item class="form-item" name="breakfast">
							<view class="form-item-title d-flex a-center">
								<view class="item-title-left"></view>
								<view class="item-title-text">推荐饮食</view>
							</view>
							<view class="item-list d-flex d-c j-center">
								<view class="d-flex j-between">
									<view class="item-list-left d-flex d-c a-center j-center">
										<i class="icon icon-zaocan" style="color: #333333;font-size: 40rpx;"></i>
										<view class="list-left-title">早餐</view>
									</view>
									<view @tap.stop="goProgrammeFood(0)" v-if="formData.breakfast.length==0" class="item-list-right d-flex j-end">
										<view  class="list-right-add d-flex a-center">
											<i class="icon icon-tianjia" style="color: #1DC691;font-size: 50rpx;"></i>
										</view>
									</view>
									<view @tap.stop="goProgrammeFood(0)" v-else  class="item-list-right d-flex d-c j-center" style="padding: 0 20rpx 0 0;">
										<template v-for="(item) in formData.breakfast">
											<view v-if="item.show" class="list-right-row d-flex a-center j-between">
												<view class="d-flex a-center">
													<!--
													<view class="right-row-circle"></view>
													-->
													<view v-if="item.healthLight=='1'||item.healthLight==null" class="right-row-circle" style="background: #1DC691;"></view>
													<view v-if="item.healthLight=='2'" class="right-row-circle" style="background: #FFAF67;"></view>
													<view v-if="item.healthLight=='3'" class="right-row-circle" style="background: #FF5831;"></view>
													<view class="right-row-name">{{item.foodName}}</view>
												</view>
												<view class="right-row-unit d-flex a-center">{{item.eatHeatQuantity}}千卡/{{item.eatGrams}}{{item.unit}}</view>
											</view>
										</template>
									</view>
								</view>
								<view v-show="isShowMore(formData.breakfast).show" @tap.stop="expand(0)" class="item-list-more">更多(剩{{isShowMore(formData.breakfast).size}}项)</view>
							</view>
							<view style="height: 20rpx;"></view>
						</uni-forms-item>
						<uni-forms-item class="form-item" name="lunch">
							<view class="item-list d-flex d-c j-center">
								<view class="d-flex j-between">
									<view class="item-list-left d-flex d-c a-center j-center">
										<i class="icon icon-wucan" style="color: #333333;font-size: 40rpx;"></i>
										<view class="list-left-title">午餐</view>
									</view>
									<view @tap.stop="goProgrammeFood(1)" v-if="formData.lunch.length==0" class="item-list-right d-flex j-end">
										<view class="list-right-add d-flex a-center">
											<i class="icon icon-tianjia" style="color: #1DC691;font-size: 50rpx;"></i>
										</view>
									</view>
									<view @tap.stop="goProgrammeFood(1)" v-else class="item-list-right d-flex d-c j-center" style="padding: 0 20rpx 0 0;">
										<template v-for="(item) in formData.lunch">
											<view v-if="item.show" class="list-right-row d-flex a-center j-between">
												<view class="d-flex a-center">
													<view v-if="item.healthLight=='1'||item.healthLight==null" class="right-row-circle" style="background: #1DC691;"></view>
													<view v-if="item.healthLight=='2'" class="right-row-circle" style="background: #FFAF67;"></view>
													<view v-if="item.healthLight=='3'" class="right-row-circle" style="background: #FF5831;"></view>
													<view class="right-row-name">{{item.foodName}}</view>
												</view>
												<view class="right-row-unit d-flex a-center">{{item.eatHeatQuantity}}千卡/{{item.eatGrams}}{{item.unit}}</view>
											</view>
										</template>
									</view>
								</view>
								<view v-show="isShowMore(formData.lunch).show" @tap.stop="expand(1)" class="item-list-more">更多(剩{{isShowMore(formData.lunch).size}}项)</view>
							</view>
							<view style="height: 20rpx;"></view>
						</uni-forms-item>
						<uni-forms-item class="form-item" name="dinner">
							<view class="item-list d-flex d-c j-center">
								<view class="d-flex j-between">
									<view class="item-list-left d-flex d-c a-center j-center">
										<i class="icon icon-wucan" style="color: #333333;font-size: 40rpx;"></i>
										<view class="list-left-title">晚餐</view>
									</view>
									<view @tap.stop="goProgrammeFood(2)" v-if="formData.dinner.length==0" class="item-list-right d-flex j-end">
										<view class="list-right-add d-flex a-center">
											<i class="icon icon-tianjia" style="color: #1DC691;font-size: 50rpx;"></i>
										</view>
									</view>
									<view @tap.stop="goProgrammeFood(2)" v-else class="item-list-right d-flex d-c j-center" style="padding: 0 20rpx 0 0;">
										<template v-for="(item) in formData.dinner">
											<view v-if="item.show" class="list-right-row d-flex a-center j-between">
												<view class="d-flex a-center">
													<view v-if="item.healthLight=='1'||item.healthLight==null" class="right-row-circle" style="background: #1DC691;"></view>
													<view v-if="item.healthLight=='2'" class="right-row-circle" style="background: #FFAF67;"></view>
													<view v-if="item.healthLight=='3'" class="right-row-circle" style="background: #FF5831;"></view>
													<view class="right-row-name">{{item.foodName}}</view>
												</view>
												<view class="right-row-unit d-flex a-center">{{item.eatHeatQuantity}}千卡/{{item.eatGrams}}{{item.unit}}</view>
											</view>
										</template>
									</view>
								</view>
								<view v-show="isShowMore(formData.dinner).show" @tap.stop="expand(2)" class="item-list-more">更多(剩{{isShowMore(formData.dinner).size}}项)</view>
							</view>
							<view style="height: 20rpx;"></view>
						</uni-forms-item>
						<uni-forms-item class="form-item" name="addFood">
							<view class="item-list d-flex d-c j-center">
								<view class="d-flex j-between">
									<view class="item-list-left d-flex d-c a-center j-center">
										<i class="icon icon-jiacan" style="color: #333333;font-size: 40rpx;"></i>
										<view class="list-left-title">加餐</view>
									</view>
									<view @tap.stop="goProgrammeFood(3)" v-if="formData.addFood.length==0" class="item-list-right d-flex j-end">
										<view class="list-right-add d-flex a-center">
											<i class="icon icon-tianjia" style="color: #1DC691;font-size: 50rpx;"></i>
										</view>
									</view>
									<view @tap.stop="goProgrammeFood(3)" v-else class="item-list-right d-flex d-c j-center" style="padding: 0 20rpx 0 0;">
										<template v-for="(item) in formData.addFood">
											<view v-if="item.show" class="list-right-row d-flex a-center j-between">
												<view class="d-flex a-center">
													<view v-if="item.healthLight=='1'||item.healthLight==null" class="right-row-circle" style="background: #1DC691;"></view>
													<view v-if="item.healthLight=='2'" class="right-row-circle" style="background: #FFAF67;"></view>
													<view v-if="item.healthLight=='3'" class="right-row-circle" style="background: #FF5831;"></view>
													<view class="right-row-name">{{item.foodName}}</view>
												</view>
												<view class="right-row-unit d-flex a-center">{{item.eatHeatQuantity}}千卡/{{item.eatGrams}}{{item.unit}}</view>
											</view>
										</template>
									</view>
								</view>
								<view v-show="isShowMore(formData.addFood).show" @tap.stop="expand(3)" class="item-list-more">更多(剩{{isShowMore(formData.addFood).size}}项)</view>
							</view>
							<view style="height: 30rpx;border-bottom: 2rpx solid #F5F5F5;"></view>
						</uni-forms-item>
						<uni-forms-item class="form-item" name="sports">
							<view class="form-item-title d-flex a-center">
								<view class="item-title-left"></view>
								<view class="item-title-text">推荐运动</view>
							</view>
							<view class="item-list d-flex d-c j-center">
								<view class="d-flex j-between">
									<view class="item-list-left d-flex d-c a-center j-center">
										<i class="icon icon-yundong" style="color: #333333;font-size: 40rpx;"></i>
										<view class="list-left-title">运动</view>
									</view>
									<view @tap.stop="goProgrammeSports" v-if="formData.sports.length==0" class="item-list-right d-flex j-end">
										<view class="list-right-add d-flex a-center">
											<i class="icon icon-tianjia" style="color: #1DC691;font-size: 50rpx;"></i>
										</view>
									</view>
									<view @tap.stop="goProgrammeSports" v-else class="item-list-right d-flex d-c j-center" style="padding: 0 20rpx 0 0;">
										<template v-for="(item) in formData.sports">
											<view v-if="item.show" class="list-right-row d-flex a-center j-between">
												<view class="d-flex a-center">
													<image v-if="item.heatLevel=='1'" style="width: 30rpx;height: 30rpx;" src="@/static/image/fire-3.png"></image>
													<image v-if="item.heatLevel=='2'" style="width: 30rpx;height: 30rpx;" src="@/static/image/fire-2.png"></image>
													<image v-if="item.heatLevel=='3'" style="width: 30rpx;height: 30rpx;" src="@/static/image/fire-1.png"></image>
													<!--
													<view class="right-row-circle"></view>
													-->
													<view class="right-row-name">{{item.name}}</view>
												</view>
												<view class="right-row-unit d-flex a-center">{{item.heat}}千卡/{{item.time}}{{item.unit}}</view>
											</view>
										</template>
									</view>
								</view>
								<view v-show="isShowMore(formData.sports).show" @tap.stop="expand(4)" class="item-list-more">更多(剩{{isShowMore(formData.sports).size}}项)</view>
							</view>
							<view style="height: 20rpx;"></view>
						</uni-forms-item>
					</view>
					<view style="height: 20rpx;"></view>
					<view class="content-form">
						<view class="form-item-title d-flex a-center">
							<image style="width: 40rpx;height: 40rpx;" src="@/static/image/cup.png"></image>
							<view class="item-title-text" style="font-size: 24rpx;">饮水</view>
						</view>
						<view class="form-item-describe">充足的饮水是减肥过程中重要的组成环节，除身体有特殊情况不能过多饮水外，按照轻盈推荐的饮水量执行即可。</view>
					</view>
				</uni-forms>
			</view>
			<view class="bottom-btn d-flex j-center">
				<tm-button
				    _style="margin: 0;font-weight: 600;"
				    :width="690"
				    :height="80"
				    :shadow="-1"
				    round="5"
				    :linear-color="['#64F1C5', '#29DBA3']"
				    linear="left"
				    :fontSize="32"
				    fontColor="#ffffff"
				    label="下一步"
					@tap="verification"
				></tm-button>
			</view>
		</scroll-view>
		<!--热量差选择-->
		<calorie ref="caloriePopup" @onGetCalorie="onGetCalorie" />
		<!--BMI计算对照-->
		<bmi ref="bmiPopup"/>
		<!--退出提示控件-->
		<uni-popup ref="backPopup" :safe-area="false">
		    <view class="sys-confirm-content">
				<view class="confirm-content-title">是否退出编辑？（内容在退出时自动存为草稿）</view>
				<!--
				<view class="confirm-content-tip">
					<view class="content-tip-row"  style="color: #1DC691;">该方案的饮食和运动将显示在饮食和运动项的首位。</view>
				</view>
				-->
				<view  @tap="backConfirm" class="content-button button-confirm" style="color: #1DC691;">确认</view>
				<view style="height: 20rpx; background: #f5f5f5"></view>
				<view @tap="backPopup.close()" class="content-button" style="color: #999999;">取消</view>
		    </view>
		</uni-popup>
		<!--引用提示控件-->
		<uni-popup ref="continuePopup" :safe-area="false">
		    <view class="sys-confirm-content">
				<view class="confirm-content-title">是否继续上次编辑？</view>
				<!--
				<view class="confirm-content-tip">
					<view class="content-tip-row"  style="color: #1DC691;">该方案的饮食和运动将显示在饮食和运动项的首位。</view>
				</view>
				-->
				<view  @tap="continueConfirm" class="content-button button-confirm" style="color: #1DC691;">继续</view>
				<view style="height: 20rpx; background: #f5f5f5"></view>
				<view @tap="continueClear" class="content-button" style="color: #999999;">清空</view>
		    </view>
		</uni-popup>
		<!--下一步警告控件-->
		<uni-popup ref="warnPopup" :safe-area="false">
		    <view class="sys-confirm-content">
				<view class="confirm-content-title">当前方案还缺少以下信息，确认不填写进入下一步？</view>
				<view class="confirm-content-tip">
					<view class="content-tip-row" style="color: #cc3300;"  v-if="warnFood!=''">{{warnFood}}饮食推荐</view>
					<view class="content-tip-row" style="color: #cc3300;"  v-if="formData.sports.length==0">运动推荐</view>
				</view>
				<view  @tap="nextStep" class="content-button button-confirm" style="color: #1DC691;">确认</view>
				<view style="height: 20rpx; background: #f5f5f5"></view>
				<view @tap="warnPopup.close()" class="content-button" style="color: #999999;">取消</view>
		    </view>
		</uni-popup>
	</view>
    <statement></statement>
</template>

<script setup lang="tsx" name="programmeCreate">
import { ref,nextTick } from "vue";
import { onLoad,onUnload } from '@dcloudio/uni-app';
import calorie from "./components/calorie.vue";
import bmi from "./components/bmi.vue";
import { getBodyInfo } from "@/api/api";
import { userData as userDataStore } from "@/store/userData";
import BigNumber from "bignumber.js";
import statement from "@/components/statement.vue";

const userData: any = userDataStore();
const backPopup = ref();
const goBack = () => {
	/*
	if(formData.value.isAdd){
		if(isVlue()){
			backPopup.value.open("bottom");
		}else{
			uni.removeStorageSync('programmeCreate');
			uni.navigateBack();
		}
	}else{
		uni.navigateBack();
	}
	*/
	uni.navigateBack();
	
};
onUnload(()=>{
	if(formData.value.isAdd){
		if(isVlue()){
			uni.setStorageSync("programmeCreate",formData.value);
			uni.showToast({
			    icon: "none",
			    title: '草稿已保存',
			    duration: 1500,
			});
		}else{
			uni.removeStorageSync('programmeCreate');
		}
	}
	//console.log("返回1")
});

const backConfirm = () => {
	uni.setStorageSync("programmeCreate",formData.value);
	uni.navigateBack();
}
const continuePopup = ref();
const continueConfirm = () => {
	const value = uni.getStorageSync('programmeCreate');
	formData.value = value;
	continuePopup.value.close();
}
const continueClear = () => {
	uni.removeStorageSync('programmeCreate');
	continuePopup.value.close();
}
const bmiList:any = ref(["<18.5","18.5-23.9","24-29.9","30-34.9",">34.9"]);
const bmiSelect:any = ref([1]);
const form = ref();
const formData:any = ref({
	name:"",//方案名称
	introduction:"",//方案简介
	bmi:bmiList.value[1],//BMI
	heatIsPoor:"",
	breakfast:[],
	lunch:[],
	dinner:[],
	addFood:[],
	sports:[],
	photo:"",
	relayStatus:1,
	isPublic:0,
	isAdd:true
});
const rules: any = ref({
	name: {
		rules: [{
				required: true,
				errorMessage: "请输入方案名称",
			},
			{
				maxLength: 20,
				errorMessage: "最多20个字",
			},
		]
	},
	introduction: {
		rules: [{
				required: true,
				errorMessage: "请输入方案简介",
			},
			{
				minLength: 30,
				errorMessage: "方案介绍不少于30字",
			},
		]
	},
	heatIsPoor: {
		rules: [{
				required: true,
				errorMessage: "请设置热量差",
			},
		]
	},
	/*
	breakfast:{
		rules: [{
				format: 'array'
			},
			{
				validateFunction: function(rule:any, value:any, data:any, callback:any) {
					if (value.length < 1) {
						callback('请选择早餐食物')
					}
					return true
				}
			}
		]
	},
	lunch:{
		rules: [{
				format: 'array'
			},
			{
				validateFunction: function(rule:any, value:any, data:any, callback:any) {
					if (value.length < 1) {
						callback('请选择午餐食物')
					}
					return true
				}
			}
		]
	},
	dinner:{
		rules: [{
				format: 'array'
			},
			{
				validateFunction: function(rule:any, value:any, data:any, callback:any) {
					if(formData.value.breakfast.length==0&&formData.value.lunch.length==0&&formData.value.dinner.length==0&&formData.value.addFood.length==0){
						callback('请选择加餐食物')
					}
					return true
				}
			}
		]
	},
	*/
	addFood:{
		rules: [{
				format: 'array'
			},
			{
				validateFunction: function(rule:any, value:any, data:any, callback:any) {
					if(formData.value.breakfast.length==0&&formData.value.lunch.length==0&&formData.value.dinner.length==0&&formData.value.addFood.length==0){
						callback('您还没有设置推荐饮食')
					}
					return true
				}
			}
		]
	},
	/*
	sports:{
		rules: [{
				format: 'array'
			},
			{
				validateFunction: function(rule:any, value:any, data:any, callback:any) {
					if (value.length < 1) {
						callback('您还没有设置推荐运动')
					}
					return true
				}
			}
		]
	}
	*/
})

onLoad((options:any)=>{
	let infoData = options.data;
	if(infoData!=null&&infoData!=""){
		formData.value.isAdd = false;
		
		let infoDataJson = JSON.parse(infoData);
		formData.value.id = infoDataJson.id;
		formData.value.name = infoDataJson.name;
		formData.value.introduction = infoDataJson.introduction;
		formData.value.isPublic = infoDataJson.isPublic;
		
		if(infoDataJson.heatIsPoorStart == infoDataJson.heatIsPoorEnd){
			formData.value.heatIsPoor = infoDataJson.heatIsPoorStart;
		}else{
			formData.value.heatIsPoor = infoDataJson.heatIsPoorStart+"-"+infoDataJson.heatIsPoorEnd;
		}
		countSlimming();
		
		//formData.value.heatIsPoor = infoDataJson.heatIsPoorStart+"-"+infoDataJson.heatIsPoorEnd;
		let breakfastList = infoDataJson.userPlanDetailList.filter((item:any) => item.type === 1);
		breakfastList.forEach((item:any) => {
			formData.value.breakfast.push({
				eatGrams:item.targetQuantity,
				eatHeatQuantity:item.targetHeat,
				foodHeat:item.targetHeat,
				foodId:item.targetId,
				healthLight:item.targetHeatLevel,
				foodName:item.targetName,
				unit:item.unit,
			});
		});
		setShow(formData.value.breakfast);
		let lunchList = infoDataJson.userPlanDetailList.filter((item:any) => item.type === 2);
		lunchList.forEach((item:any) => {
			formData.value.lunch.push({
				eatGrams:item.targetQuantity,
				eatHeatQuantity:item.targetHeat,
				foodHeat:item.targetHeat,
				foodId:item.targetId,
				healthLight:item.targetHeatLevel,
				foodName:item.targetName,
				unit:item.unit,
			});
		});
		setShow(formData.value.lunch);
		let dinnerList = infoDataJson.userPlanDetailList.filter((item:any) => item.type === 3);
		dinnerList.forEach((item:any) => {
			formData.value.dinner.push({
				eatGrams:item.targetQuantity,
				eatHeatQuantity:item.targetHeat,
				foodHeat:item.targetHeat,
				foodId:item.targetId,
				healthLight:item.targetHeatLevel,
				foodName:item.targetName,
				unit:item.unit,
			});
		});
		setShow(formData.value.dinner);
		let addFoodList = infoDataJson.userPlanDetailList.filter((item:any) => item.type === 4);
		addFoodList.forEach((item:any) => {
			formData.value.addFood.push({
				eatGrams:item.targetQuantity,
				eatHeatQuantity:item.targetHeat,
				foodHeat:item.targetHeat,
				foodId:item.targetId,
				healthLight:item.targetHeatLevel,
				foodName:item.targetName,
				unit:item.unit,
			});
		});
		setShow(formData.value.addFood);
		let sportsList = infoDataJson.userPlanDetailList.filter((item:any) => item.type === 6);
		sportsList.forEach((item:any) => {
			formData.value.sports.push({
				heatLevel:item.targetHeatLevel,
				heat:item.targetHeat,
				time:item.targetQuantity,
				code:item.targetId,
				name:item.targetName,
				unit:item.unit,
			});
		});
		setShow(formData.value.sports);
		
		formData.value.photo = infoDataJson.photo;
		formData.value.relayStatus = infoDataJson.relayStatus;
		//formData.value.isPublic = infoDataJson.isPublic;
		//选中BMI
		formData.value.bmi = infoDataJson.bmi;
		if(formData.value.bmi.indexOf("<") >-1){
			formData.value.bmi.split("<");
			//console.log(formData.value.bmi.split("<"));
		}
		//console.log(JSON.parse(infoData));
		//BMI选中设置
		formData.value.bmi = infoDataJson.bmi;
		if(formData.value.bmi.indexOf("<")==-1&&formData.value.bmi.indexOf(">")==-1){
			let str = formData.value.bmi.split("-");
			let result = [];
			let start = bmiList.value.findLastIndex((val:string) => val.includes(str[0]));  
			let end = bmiList.value.findIndex((val:string) => val.includes(str[1]));  
			for(let i=start;i<=end;i++){
				result.push(i);
			}
			bmiSelect.value = result;
		}
		if(formData.value.bmi.indexOf("<")>-1){
			let str = formData.value.bmi.split("<");
			let result = [];
			let start = 0;  
			let end = bmiList.value.findIndex((val:string) => val.includes(str[1]));  
			for(let i=start;i<=end;i++){
				result.push(i);
			}
			bmiSelect.value = result;
		}
		if(formData.value.bmi.indexOf(">")>-1){
			let str = formData.value.bmi.split(">");
			let result = [];
			let start = bmiList.value.findLastIndex((val:string) => val.includes(str[1]));  
			let end = bmiList.value.length-1;
			for(let i=start;i<=end;i++){
				result.push(i);
			}
			bmiSelect.value = result;
		}
	}else{
		nextTick(() => {
			const value = uni.getStorageSync('programmeCreate');
			if (value!="") {
				continuePopup.value.open("bottom");
			}
		});
	}
	
});

//判断MBMI是否选中
const isBMI = (index:number) => {
	return bmiSelect.value.indexOf(index)>-1;
}
//BMI选择
const onBMI = (index:number) => {
	let idx = bmiSelect.value.indexOf(index);
	//添加
	if(idx==-1){
		if(bmiSelect.value.length==0){
			bmiSelect.value.push(index);
		}else{
			if(bmiSelect.value.length<3){
				let min = Math.min(...bmiSelect.value);
				let max = Math.max(...bmiSelect.value);
				if((min-index==-1||min-index==1)||max-index==-1||max-index==1){
					bmiSelect.value.push(index);
				}else{
					uni.showToast({
					    icon: "none",
					    title: '必须选择相邻的BMI段',
					    duration: 1500,
					});
				}
			}else{
				uni.showToast({
				    icon: "none",
				    title: 'BMI范围不可超过3段',
				    duration: 1500,
				});
			}
		}
	}else{//取消
		let min = bmiSelect.value.indexOf(index-1);
		let max = bmiSelect.value.indexOf(index+1);
		if(min>-1&&max>-1){
			uni.showToast({
			    icon: "none",
			    title: '请选择相邻的BMI段',
			    duration: 1500,
			});
		}else{
			bmiSelect.value.splice(idx, 1)
		}
	}
	//根据选中内容更新值
	if(bmiSelect.value.length==0){
		formData.value.bmi = "";
	}else{
		if(bmiSelect.value.indexOf(0)>-1){
			if(bmiSelect.value.length == 1){
				formData.value.bmi = bmiList.value[0];
			}else{
				let max = Math.max(...bmiSelect.value);
				formData.value.bmi = "<"+bmiList.value[max].split("-")[1]; 
			}
			
		}
		if(bmiSelect.value.indexOf(bmiList.value.length-1)>-1){
			if(bmiSelect.value.length == 1){
				formData.value.bmi = bmiList.value[bmiList.value.length-1];
			}else{
				let min = Math.min(...bmiSelect.value);
				formData.value.bmi = ">"+bmiList.value[min].split("-")[0]; 
			}
			
		}
		if(bmiSelect.value.indexOf(0)==-1&&bmiSelect.value.indexOf(bmiList.value.length-1)==-1){
			if(bmiSelect.value.length == 1){
				formData.value.bmi = bmiList.value[bmiSelect.value[0]];
			}else{
				let min = Math.min(...bmiSelect.value);
				let max = Math.max(...bmiSelect.value);
				formData.value.bmi = bmiList.value[min].split("-")[0]+"-"+bmiList.value[max].split("-")[1]; 
			}
		}
	}
	
}
//热量差选择
const caloriePopup = ref();
const showCaloriePopup = () => {
	let val = formData.value.heatIsPoor;
	//console.log(val)
	if(val!=null&&val!=""&&val.toString().indexOf("-") == -1){
		val = val+"-"+val;
	}
	caloriePopup.value.showPopup(val);
}
const onGetCalorie = (e: any) => {
	let val = e.split("-");
	if(val[0]==val[1]){
		formData.value.heatIsPoor = val[0];
	}else{
		formData.value.heatIsPoor = e;
	}
	countSlimming();
}
const heatIsPoorDescribe = ref();
//根据热量差估算每周减重数量
const countSlimming = () => {
	
	getBodyInfo({userId:userData.id}).then((res:any) => {
		//console.log(res.data)
		let rest = countRest(res.data);
		
		let ratio = 7.7; //热量比率
		if(formData.value.heatIsPoor.toString().indexOf("-") == -1){
			//let min = parseFloat((Number(formData.value.heatIsPoor) / ratio * 7 / 1000).toFixed(2));
			
			let min = Number(new BigNumber(Number(formData.value.heatIsPoor)).plus(new BigNumber(rest).times(new BigNumber(0.25))).div(new BigNumber(ratio)).div(new BigNumber(1000)).times(new BigNumber(7)).toNumber().toFixed(2));
			
			heatIsPoorDescribe.value = `(据你的基础代谢，每周可减约${min}kg)`;
		}else{
			let list = formData.value.heatIsPoor.split("-");
			//每周减重千克数
			//let min = parseFloat((Number(list[0]) / ratio * 7 / 1000).toFixed(2));
			let min = Number(new BigNumber(Number(list[0])).plus(new BigNumber(rest).times(new BigNumber(0.25))).div(new BigNumber(ratio)).div(new BigNumber(1000)).times(new BigNumber(7)).toNumber().toFixed(2));
			
			//let max = parseFloat((Number(list[1]) / ratio * 7 / 1000).toFixed(2));
			let max = Number(new BigNumber(Number(list[1])).plus(new BigNumber(rest).times(new BigNumber(0.25))).div(new BigNumber(ratio)).div(new BigNumber(1000)).times(new BigNumber(7)).toNumber().toFixed(2));
			heatIsPoorDescribe.value = `(据你的基础代谢，每周可减约${min}-${max}kg)`;
		}
		
	});
	
	
}
const goProgrammeFood = (type:number) => {
	uni.$once('programmeFood',(data:any) => {
		if(data.type==0){
			formData.value.breakfast = data.food;
			setShow(formData.value.breakfast);
			
		}
		if(data.type==1){
			formData.value.lunch = data.food;
			setShow(formData.value.lunch);
		}
		if(data.type==2){
			formData.value.dinner = data.food;
			setShow(formData.value.dinner);
		}
		if(data.type==3){
			formData.value.addFood = data.food;
			setShow(formData.value.addFood);
		}
	})
	let food = "[]";
	if(type==0){
		food = JSON.stringify(formData.value.breakfast);
	}
	if(type==1){
		food = JSON.stringify(formData.value.lunch);
	}
	if(type==2){
		food = JSON.stringify(formData.value.dinner);
	}
	if(type==3){
		food = JSON.stringify(formData.value.addFood);
	}
	uni.navigateTo({
	    url: `/pages/programmeFood/programmeFood?type=${type}&food=${food}`,
	});
}
//设置超过五个影藏
const setShow = (list:any) => {
	list.forEach((res:any,index:number) => {
		if(index<5){
			res.show = true;
		}else{
			res.show = false;
		}
	});
}
//判断是否显示更多
const isShowMore = (list:any) => {
	let json = {
		show:false,
		size:0
	}
	let results:any = list.filter((item:any) => !item.show);
	if(results.length>0){
		json.show = true;
		json.size = results.length;
	}
	return json;
}
//展开更多


const expand = (type:number) => {
	if(type==0){
		formData.value.breakfast.forEach((res:any) => {
			res.show = true;
		});
	}
	if(type==1){
		formData.value.lunch.forEach((res:any) => {
			res.show = true;
		});
	}
	if(type==2){
		formData.value.dinner.forEach((res:any) => {
			res.show = true;
		});
	}
	if(type==3){
		formData.value.addFood.forEach((res:any) => {
			res.show = true;
		});
	}
	if(type==4){
		formData.value.sports.forEach((res:any) => {
			res.show = true;
		});
	}
}
const goProgrammeSports = () => {
	uni.$once('programmeSports',(data:any) => {
		formData.value.sports = data.sports;
		setShow(formData.value.sports);
		//console.log(formData.value.sports)
	});
	uni.navigateTo({
	    url: `/pages/programmeSports/programmeSports?sports=${JSON.stringify(formData.value.sports)}`,
	});
}
const bmiPopup = ref();
const showBmi = () => {
	bmiPopup.value.open();
}
const warnPopup = ref();
const warnFood = ref();
const verification = () => {
	warnFood.value = "";
	form.value.validate().then((res:any)=>{
		if(formData.value.breakfast.length==0||formData.value.lunch.length==0||formData.value.dinner.length==0||formData.value.addFood.length==0||formData.value.sports.length==0){
			//callback('您还没有设置推荐饮食')
			if(formData.value.breakfast.length==0){
				warnFood.value += "早餐/";
			}
			if(formData.value.lunch.length==0){
				warnFood.value += "午餐/";
			}
			if(formData.value.dinner.length==0){
				warnFood.value += "晚餐/";
			}
			if(formData.value.addFood.length==0){
				warnFood.value += "加餐/";
			}
			//console.log(warnFood.value)
			warnFood.value  = warnFood.value.slice(0, -1);
			warnPopup.value.open("bottom");
		}else{
			nextStep();
		}
	}).catch((err:any) =>{
		console.log('表单错误信息：', err);
	});
}
//下一步
const nextStep = () => {
	warnPopup.value.close();
	uni.$once('setFormData',(data:any) => {
		formData.value = data;
	});
	uni.navigateTo({
	    url: `/pages/programmeCover/programmeCover?formData=${JSON.stringify(formData.value)}`,
	});
}
//计算静息消耗
const countRest = (data:any) => {
	let consumeRest = 0;
	let w = data.userHealth.initWeight;
	if(data.userWeightRecord!=null)w = data.userWeightRecord.weight;
    if (data.userHealth.sex === 1) {
		
        let weight = new BigNumber(13.73).multipliedBy(
			new BigNumber(w)
        );
        let height = new BigNumber(5).multipliedBy(
			new BigNumber(data.userHealth.stature)
        );
        let age = new BigNumber(6.9).multipliedBy(
			new BigNumber(data.userHealth.age)
		);
        consumeRest = Math.round(
            new BigNumber(67).plus(weight).plus(height).minus(age).toNumber()
        );
		//console.log(consumeRest.value)
    }
    if (data.userHealth.sex === 2) {
        let weight = new BigNumber(9.6).multipliedBy(
            new BigNumber(w)
        );
        let height = new BigNumber(1.72).multipliedBy(
            new BigNumber(data.userHealth.stature)
        );
        let age = new BigNumber(4.7).multipliedBy(
			new BigNumber(data.userHealth.age)
		);
        consumeRest = Math.round(
            new BigNumber(661).plus(weight).plus(height).minus(age).toNumber()
        );
    }
	return consumeRest;
};
//判断是否填的有值
const isVlue = () =>{
	if(formData.value.name==''&&
		formData.value.introduction==''&&
		formData.value.heatIsPoor==''&&
		formData.value.breakfast.length==0&&
		formData.value.lunch.length==0&&
		formData.value.dinner.length==0&&
		formData.value.addFood.length==0&&
		formData.value.sports.length==0&&
		formData.value.photo==''
	){
		return false;
	}else{
		return true;
	}
}
</script>

<style lang="scss" scoped>
@import './programmeCreate.scss';
</style>